package com.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.controller.vo.ResultVO;
import com.entity.AlipayConfigEnum;
import com.entity.Order;
import com.exception.UpdateException;
import com.service.AlipayService;
import com.service.OrderService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName AlipayController
 * @Description 支付宝控制器
 * @Author Kx
 * @Date 2025/6/3 19:11
 * @Version 1.0
 */
@RestController
@RequestMapping("/alipay")
public class AlipayController extends BaseController {
    private final AlipayService alipayService;
    private final OrderService orderService;

    public AlipayController(AlipayService alipayService, OrderService orderService) {
        this.alipayService = alipayService;
        this.orderService = orderService;
    }

    /**
     * @param orderId : 订单编号
     * @param amount  : 支付金额
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @author: kx
     * @date: 2025/6/3 20:44
     * @description:
     */
    @PostMapping("/createOrder")
    public ResultVO<String> createOrder(@RequestParam("orderId") String orderId, @RequestParam("amount") String amount) {
        // 调用支付宝服务获取支付二维码
        String order = alipayService.createOrder(orderId, amount);
        return success(order);
    }

    @PostMapping("/handleAliPayNotify")
    public String handleAliPayNotify(@RequestParam Map<String, String> params) {
        System.out.println("收到异步通知" + params);
        try {
            // 1. 验签
            if (AlipaySignature.rsaCheckV1(params, AlipayConfigEnum.getInstance().getAlipayPublicKey(), "UTF-8", "RSA2")) {
                // 2. 校验通知类型和交易状态
                String tradeStatus = params.get("trade_status");
                System.out.println("交易状态：" + tradeStatus);
                if ("TRADE_SUCCESS".equals(tradeStatus)) {
                    // 获取订单编号
                    String orderId = params.get("out_trade_no");
                    // 修改订单状态
                    try {
                        orderService.updateOrderStatus(orderId, 1,null);
                    } catch (Exception e) {
                        throw new UpdateException(1003, "修改订单状态失败");
                    }
                }
                return "success"; // 必须返回 success
            } else {
                System.out.println("验签失败");
                return "fail";
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return "fail";
        }
    }

    /**
     * @author: kx
     * @date: 2025/6/4 8:45
     * @param orderId : 订单编号
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @description: 根据订单编号查询客户支付状态
    */
    @GetMapping("/status")
    public ResultVO<String> status(@RequestParam("orderId") String orderId) {
        // 获取订单信息
        Order order = orderService.getOrderById(orderId);
        // 判断是否支付成功
        if (order.getOrderStatus() == 1) {
            return success("支付成功");
        }
        return success("支付失败");
    }
}
